<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件</title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //声明一个状态
            //1. 声明状态属性
            state = {
                username: '',
                password: ''
            }

            render(){
                return <div>
                        <h2>登录</h2>
                        <hr />
                        {/**2. 在 表单元素中, 设置 value 与 state 中的属性关联**/}
                        用户名: <input onChange={this.getUsername} type="text" value={this.state.username}/><br/>
                        密码: <input onChange={this.getPassword}  type="password"  value={this.state.password} /><br/>
                        密码: <input onChange={this.getPassword}  type="password"  value={this.state.password} /><br/>
                        <input type="submit" value="登录" onClick={this.login} />
                    </div>
            }

            //设置用户名的回调
            getUsername = (e) => {
                //3. 修改 state 中 username 的值
                this.setState({
                    username: e.target.value
                })
            }
            //设置密码的回调
            getPassword = (e) => {
                this.setState({
                    password: e.target.value
                })
            }
        
            login = () => {
                //4. 获取输出用户名和密码的值
                let str = `用户名为: ${this.state.username} 密码为: ${this.state.password}`;
                console.log(str);
            }
        }

        ReactDOM.render(<App/>, document.querySelector("#root"));
    </script>
</body>
</html>